package com.telsoft.data;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.List;
import java.util.Vector;

import com.telsoft.business.DAOHelperBusiness;
import com.telsoft.entity.LibraryEntity;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.sql.PreparedStatement;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.faces.context.FacesContext;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpSession;
import oracle.sql.BLOB;

public class LibraryDAO {

    public List<LibraryEntity> getLibrary() throws SQLException {
        List<LibraryEntity> librarys = new ArrayList<LibraryEntity>();
        Connection conn = DAOHelperBusiness.getConnection();
        Statement stm = DAOHelperBusiness.getStatement(conn);
        String sql = "SELECT * FROM LIBRARY";
        ResultSet rs = stm.executeQuery(sql);
        while (rs.next()) {
            LibraryEntity lib = new LibraryEntity();
            lib.setLibID(rs.getInt("LIB_ID"));
            lib.setName(rs.getString("NAME"));
            lib.setCompany(rs.getString("COMPANY"));
            lib.setDetail(rs.getString("DETAIL"));
            lib.setVersion(rs.getString("VERSION"));
            librarys.add(lib);
        }
        DAOHelperBusiness.closeStatement(stm);
        DAOHelperBusiness.closeConnection(conn);
        return librarys;
    }

    public String getFileByFileID(int file_id) throws FileNotFoundException, IOException {
        try {
            Connection conn = DAOHelperBusiness.getConnection();
            PreparedStatement stmt;
            ResultSet rs = null;
            String StrSQL = "SELECT CONTENT,NAME FROM LIBRARY WHERE LIB_ID=?";
            stmt = conn.prepareStatement(StrSQL);
            stmt.setInt(1, file_id);
            rs = stmt.executeQuery();
            if (rs.next()) {
                BLOB tmpBLOB = (BLOB) rs.getBlob(1);
                java.io.InputStream tmpStream = tmpBLOB.getBinaryStream();
                //get sessionid
                FacesContext fCtx = FacesContext.getCurrentInstance();
                HttpSession session = (HttpSession) fCtx.getExternalContext().getSession(false);
                String sessionId = session.getId();
                // end get sessionid
                ServletContext context2 = (ServletContext) FacesContext.getCurrentInstance().getExternalContext().getContext();
                String fileName = context2.getRealPath("/resources/" + rs.getString(2) + "_" + sessionId + ".xml");
                OutputStream out = new FileOutputStream(new File(fileName));
                int read;
                byte[] bytes = new byte[1024];
                while ((read = tmpStream.read(bytes)) != -1) {
                    out.write(bytes, 0, read);
                }
                tmpStream.close();
                out.flush();
                out.close();
                File file = new File(fileName);
                return fileName;
            }
            return null;
        } catch (SQLException ex) {
            ex.printStackTrace();
        }
        return null;
    }
}
